package com.github.shiqiyue.app.config.session;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.session.SessionProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.MapSessionRepository;
import org.springframework.session.SessionRepository;
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.web.http.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.session.web.http.HeaderHttpSessionIdResolver;

import java.util.HashMap;

/***
 * spring session配置
 *
 * @author wwy
 *
 */
@Configuration
public class SpringSessionConfiguration extends SpringHttpSessionConfiguration {
    @Autowired
    private SessionProperties sessionProperties;

    /***
     * 配置 spring session redis 的默认序列器
     * @return
     */
    @Bean(name = "springSessionDefaultRedisSerializer")
    public RedisSerializer redisSerializer() {
        return new GenericFastJsonRedisSerializer();
    }

    /****
     * 配置session策略
     *
     * @return
     */
    @Bean
    public MultiHttpSessionIdResolver httpSessionIdResolver() {
        MultiHttpSessionIdResolver multiHttpSessionIdResolver = new MultiHttpSessionIdResolver();

        // sapi
        DefaultCookieSerializer sapiCookieSerializer = new DefaultCookieSerializer();
        sapiCookieSerializer.setCookieName("SAPI_SESSION");
        sapiCookieSerializer.setCookiePath("/sapi");
        CookieHttpSessionIdResolver sapiCookieHttpSessionIdResolver = new CookieHttpSessionIdResolver();
        sapiCookieHttpSessionIdResolver.setCookieSerializer(sapiCookieSerializer);
        HeaderHttpSessionIdResolver sapiHeaderHttpSessionIdResolver = new HeaderHttpSessionIdResolver("sapi-token");
        CookieAndHeaderHttpSessionIdResolver sapiHttpSessionIdResolver = new CookieAndHeaderHttpSessionIdResolver(sapiCookieHttpSessionIdResolver, sapiHeaderHttpSessionIdResolver, "/sapi");
        multiHttpSessionIdResolver.addHttpSessionIdResolver(sapiHttpSessionIdResolver);
        // shopapi
        DefaultCookieSerializer shopapiCookieSerializer = new DefaultCookieSerializer();
        shopapiCookieSerializer.setCookieName("SHOP_SESSION");
        shopapiCookieSerializer.setCookiePath("/gapi");

        CookieHttpSessionIdResolver shopapiCookieHttpSessionIdResolver = new CookieHttpSessionIdResolver();
        shopapiCookieHttpSessionIdResolver.setCookieSerializer(shopapiCookieSerializer);
        HeaderHttpSessionIdResolver shopapiHeaderHttpSessionIdResolver = new HeaderHttpSessionIdResolver("shopapi-token");
        CookieAndHeaderHttpSessionIdResolver shopapiHttpSessionIdResolver = new CookieAndHeaderHttpSessionIdResolver(shopapiCookieHttpSessionIdResolver, shopapiHeaderHttpSessionIdResolver, "/gapi");
        multiHttpSessionIdResolver.addHttpSessionIdResolver(shopapiHttpSessionIdResolver);

        return multiHttpSessionIdResolver;
    }


}
